<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      size="small"
      ref="formRef"
      :model="form"
      label-width="200px"
      v-if="open_type != 'psw'"
    >
      <el-form-item label="昵称" prop="nickName">
        <el-input
          class="max-w460"
          v-model="form.nickName"
          placeholder="请输入昵称"
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="real_name">
        <el-input
          class="max-w460"
          v-model="form.real_name"
          placeholder="请输入真实姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          class="max-w460"
          v-model="form.mobile"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="头像">
        <el-row>
          <el-button icon="Picture" @click="openUpload('avatarUrl')"
            >选择图片</el-button
          >
          <div v-if="form.avatarUrl != ''" class="img">
            <img
              :src="form.avatarUrl"
              width="100"
              height="100"
              @click="openUpload('avatarUrl')"
            />
          </div>
        </el-row>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="form.gender">
          <el-radio :value="1">男</el-radio>
          <el-radio :value="0">女</el-radio>
          <el-radio :value="2">保密</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="type === 'add'">
        <el-input
          class="max-w460"
          type="password"
          show-password
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-form
      size="small"
      ref="formRef"
      :model="psw_form"
      label-width="200px"
      v-else
    >
      <el-form-item label="密码" prop="password">
        <el-input
          class="max-w460"
          type="password"
          v-model="psw_form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
    </el-form>
    <!--上传图片组件-->
    <Upload
      v-if="isupload"
      :isupload="isupload"
      :type="type"
      @returnImgs="returnImgsFunc"
      >上传图片</Upload
    >
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading"
        >确定</el-button
      >
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { adduser, editPassword, edituser, getuser } from "@/api/user.js";
import Upload from "@/components/file/Upload.vue";
import { formatModel } from "@/utils/base.js";
import { ElMessage } from "element-plus";
import { ref } from "vue";
const title = ref("编辑会员");
const dialogVisible = ref(false);
const form = ref({
  user_id: 0,
  nickName: "",
  real_name: "",
  avatarUrl: "",
  gender: 2,
  mobile: "",
  password: "",
});
const psw_form = ref({
  user_id: 0,
  password: "",
});
const formRef = ref<any>(null);
const loading = ref(false);
/*是否上传图片*/
const isupload = ref(false);
const dialogFormVisible = () => {
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (type: string, id?: number) => {
  open_type.value = type;
  if (type == "edit") {
    title.value = "编辑会员";
    getuser({ user_id: id }).then((data) => {
      let model = data.data.model;
      form.value = formatModel(form.value, model);
      dialogVisible.value = true;
    });
  } else if (type == "add") {
    title.value = "添加会员";
    dialogVisible.value = true;
  } else if (type == "psw") {
    title.value = "修改密码";
    psw_form.value = {
      user_id: id,
      password: "",
    };
    dialogVisible.value = true;
  }
};
const emits = defineEmits(["doneSave"]);
const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      if (open_type.value == "add") {
        adduser(form.value)
          .then((data: any) => {
            ElMessage({
              message: "添加成功",
              type: "success",
            });
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else if (open_type.value == "edit") {
        edituser(form.value)
          .then((data: any) => {
            ElMessage({
              message: "恭喜你，修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else {
        editPassword(psw_form.value)
          .then((data: any) => {
            ElMessage({
              message: "修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
          })
          .catch((error) => {
            loading.value = false;
          });
      }
    }
  });
};
const type = ref("");
/*上传*/
const openUpload = (e) => {
  type.value = e;
  isupload.value = true;
};
/*获取图片*/
const returnImgsFunc = (e) => {
  if (e != null && e.length > 0) {
    if (type.value == "avatarUrl") {
      form.value.avatarUrl = e[0].file_path;
    }
  }
  isupload.value = false;
};
/*取消*/
const cancelFunc = () => {
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style></style>
